ROS advertise函数参数详解

突然想知道advertise函数里第三个参数到底是什么含义,网上博客找了一圈每一个解释清楚的,这里记录一下

首先看一下官方的注释:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
* \brief Advertise a topic, simple version
*
* This call connects to the master to publicize that the node will be
* publishing messages on the given topic. This method returns a Publisher that allows you to
* publish a message on this topic.
*
* This version of advertise is a templated convenience function, and can be used like so
*
* ros::Publisher pub = handle.advertise<std_msgs::Empty>("my_topic", 1);
*
* \param topic Topic to advertise on
*
* \param queue_size Maximum number of outgoing messages to be
* queued for delivery to subscribers
*
* \param latch (optional) If true, the last message published on
* this topic will be saved and sent to new subscribers when they
* connect
*
Publisher advertise(const std::string& topic, uint32_t queue_size, bool latch = false)

这里面第三个参数 bool latch,如果设置为true,会保存发布方Publisher的最后一条消息,如果有新的订阅对象订阅该发布方时,发布方会自动把这条消息发送给订阅者;这样做的目的是降低Publisher发布的频率,提高话题使用效率,举个例子:一个发布地图的Publisher只在最开始发布了一次,当latch设置为TRUE时,如果后面有新的subscriber订阅到该发布时,就算此时发布方已经停止发布了,也会把之前发布过的最后一条消息保留发给新订阅该话题的对象。